package com.jshoperxms.controller.utils;


import com.jshoperxms.controller.utils.statickey.DefaultValue;
import com.jshoperxms.entity.BasicUserT;
import com.jshoperxms.entity.JshopbasicInfoT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


public class BaseTools {
	
	private static final Logger log = LoggerFactory.getLogger(com.jshoperxms.controller.utils.BaseTools.class);
	/**
	 *获取登录用户保存在session中的userid
	 * 
	 * @return
	 */
	public static String getAdminCreateId() {
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		BasicUserT userT = (BasicUserT) request.getSession().getAttribute(com.jshoperxms.controller.utils.statickey.StaticKey.BACK_USER_SESSION_KEY);
		if (userT!=null) {
			return userT.getId();
		}
		return DefaultValue.DEFAULTADMINID;
	}
	/**
	 *获取登录用户保存在session中的username
	 * 
	 * @return
	 */
	public static String getAdminCreateName() {
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		BasicUserT userT = (BasicUserT) request.getSession()
				.getAttribute(com.jshoperxms.controller.utils.statickey.StaticKey.BACK_USER_SESSION_KEY);
		if (userT!=null) {
			return userT.getUsername();
		}
		return DefaultValue.DEFAULTADMINNAME;
	}
	


	/**
	 * 获取店铺信息
	 * @return
	 */
	public static JshopbasicInfoT getStoreInfo(){
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		JshopbasicInfoT jT = (JshopbasicInfoT) request.getSession()
				.getAttribute(com.jshoperxms.controller.utils.statickey.StaticKey.BASIC_SHOP_INFO);
		if (jT!=null) {
			return jT;
		}
		return null;
	}

	

	
	
	/**
	 * 获取网站根目录
	 * 
	 * @return
	 */
	public static String getBasePath() {
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		String path = request.getContextPath();
		return path;
	}

}
